import React, { FC, useEffect } from 'react';
import { Modal, Form, InputNumber, Input, Select } from 'antd';
import { AccountOpItem } from '@/services/person/account';

interface CreditProps {
  visible: boolean;
  accountOpItem: Partial<AccountOpItem> | undefined;
  onSubmit: (values: AccountOpItem) => void;
  onCancel: () => void;
}

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const CreditOpForm: FC<CreditProps> = (props) => {
  const [form] = Form.useForm();
  const { visible, accountOpItem, onCancel, onSubmit } = props;

  useEffect(() => {
    if (form && !visible) {
      form.resetFields();
    }
  }, []);

  useEffect(() => {
    if (accountOpItem) {
      if("check" === accountOpItem.opType){
        accountOpItem.opAmount = accountOpItem.outAmount
      }else if("refund" === accountOpItem.opType){
        accountOpItem.opAmount = accountOpItem.billAmount
      }else{
        accountOpItem.opAmount = 0
      }
      form.setFieldsValue({
        ...accountOpItem
      });
    }
  }, [props.accountOpItem]);

  const handleOpSubmit = () => {
    if (!form) return;
    form.submit();
  };

  const handleFinish = (values: { [key: string]: any }) => {
    if (onSubmit) {
      onSubmit({ ...values} as AccountOpItem);
    }
  };

  const modalFooter = { okText: '保存', onOk: handleOpSubmit, onCancel };

  const renderContent = () => {
    return (
      <>
        <Form.Item
          label="账户名称"
          name="accountName"
        >
          <Input readOnly/>
        </Form.Item>
        <Form.Item
          label="信用额度"
          name="inAmount"
        >
          <InputNumber style={{ width: '100%' }} readOnly/>
        </Form.Item>
        <Form.Item label="已用金额" 
          name="balance"
        >
          <InputNumber
            style={{ width: '100%' }}readOnly/>
        </Form.Item>
        <Form.Item
          label={`${
            accountOpItem?.opType === 'consume'
              ? '消费金额'
              : accountOpItem?.opType === 'refund'
              ? '还款金额'
              : '可用余额'
          }`}
          name="opAmount"
          rules={[{ required: true, message: '请输入金额' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入金额"
          />
        </Form.Item>
        <Form.Item
          hidden={ accountOpItem?.opType != 'check'}
          label="账单金额" 
          name="billAmount"
        >
          <InputNumber
            hidden={ accountOpItem?.opType !=  'check'}
            style={{ width: '100%' }}
            step={1000}
            formatter={(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            placeholder="请输入金额"
          />
        </Form.Item>
        <Form.Item label="备注" name="remark">
          <Input.TextArea rows={4} placeholder="请输入备注" />
        </Form.Item>
        <Form.Item hidden={true} name="id">
          <Input hidden={true} />
        </Form.Item>
        <Form.Item hidden={true} name="opType">
          <Input hidden={true} />
        </Form.Item>
      </>
    );
  };

  return (
    <Modal
      title={`${
        accountOpItem?.opType === 'consume'
          ? '消费'
          : accountOpItem?.opType === 'refund'
          ? '还款'
          : '对账'
      }`}
      width={640}
      destroyOnClose
      open={visible}
      {...modalFooter}
    >
      <Form {...formLayout} form={form} onFinish={handleFinish}>
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default CreditOpForm;